<?php

/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2018/2/23
 * Time: 下午5:06
 */
class Cart extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->model('cart_model');
        $this->load->model('user_model');
        $this->load->model('customer_model');
        $this->load->library('form_validation');//加载表单验证类库
        $this->load->helper('json');
        $this->load->helper('common');
        $this->load->model('goods_model');
    }

    /*
     * $product_id_str  订单id(订单id之间有逗号[,]分割,例如9542,9541)
     */
    public function index()
    {
        $customer_id = $this->session->customer_id;
        $identity = $this->session->customer_info['identity'];
        $where = array(
            'customer_id' => $customer_id
        );
        //获取U-M每月所剩的账期额度和admin总共所剩的账期额度
        $data['total_quota_left'] = $this->customer_model->member_total_quota_left_month($customer_id, $identity);

        if ($identity != 1) {
            $quota = $this->customer_model->u_m_single_quota($customer_id);
            $data['total_quota'] = $quota['total_quota'];

            //获取U-M单品商品额度
            $data['single_quota'] = $quota['single_quota'];

            //获取U-M用户清单id
            $data['list_id'] = $this->customer_model->u_m_list_goods_ids($customer_id);
        }
                    //获取购物车商品
        $cart_goods = $this->db->where($where)->get('app_cart')->result_array();
        if ($cart_goods) {
            foreach ($cart_goods as $k => $v) {
                $product_id_arr[] = $v['product_id'];
            }
            $product_id_str = join(',', $product_id_arr);
            $admin_shop_user_id = $this->user_model->customer_id_to_admin_to_shop_user_id($customer_id);
            $data['data'] = $this->goods_model->get_shop_goods_info_sync_cart($product_id_str, $cart_goods,$admin_shop_user_id);
        } else {
            $data['data'] = [];
        }
        $data['identity'] = $identity;
        $this->load->view('cart/index', $data);
    }

    //加入商城购物车
    public function add_shop_cart()
    {
        $customer_id = $this->session->customer_id;
        $identity = $this->session->customer_info['identity'];
        //获取商城shop_user_id;
        $shop_user_id = $this->user_model->customer_id_to_shop_user_id($customer_id);
        //请求商城获取商品信息接口
        $ids = $this->input->post();

        if (!$ids) {
            send_json(array('code' => -1, 'data' => '', 'msg' => "请选择商品"));
        }
        foreach (explode(",", $ids['ids']) as $key => $id) {
            $where = array('id' => $id, 'customer_id' => $customer_id);

            $goods = $this->db->where($where)->get('app_cart')->row_array();
            if (!$goods) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "请选择商品"));
            }
            $goods_list[$key]['product_id'] = $goods['product_id'];
            $goods_list[$key]['number'] = $goods['num'];
        }
        $url = $this->config->item('shop_url') . "companymanagerapi/company_push_carts/";
        $data_arr['time'] = time();
        $data_arr['key'] = md5($this->config->item('token_const_wjyh') . $data_arr['time']);
        $data_arr['shop_user_id'] = $shop_user_id;
        $data_arr['goods_list'] = json_encode($goods_list);


        $res = json_decode(curl_request($url, $data_arr), true);

        if ($res['code'] == 200) {
            send_json(array('code' => 0, 'data' => '', 'msg' => $res['msg']));
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => $res['msg']));
        }


    }

    //单个加入商城购物车
    public function single_add_shop_cart()
    {
        $customer_id = $this->session->customer_id;
        $identity = $this->session->customer_info['identity'];
        //获取商城shop_user_id;
        $shop_user_id = $this->user_model->customer_id_to_shop_user_id($customer_id);
        
        $post = $this->input->post();

        if (!$post) {
            send_json(array('code' => -1, 'data' => '', 'msg' => "请选择商品"));
        }
        $goods_list['product_id'] = $post['shop_id'];
        $goods_list['number'] = $post['count'];

        $url = $this->config->item('shop_url') . "companymanagerapi/company_push_carts/";
        $data_arr['time'] = time();
        $data_arr['key'] = md5($this->config->item('token_const_wjyh') . $data_arr['time']);
        $data_arr['shop_user_id'] = $shop_user_id;
        $data_arr['goods_list'] = json_encode(array($goods_list));


        $res = json_decode(curl_request($url, $data_arr), true);

        if ($res['code'] == 200) {
            send_json(array('code' => 0, 'data' => '', 'msg' => $res['msg']));
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => $res['msg']));
        }


    }

    /*
     * 修改购物车商品选中状态
     */
    public function checked_modify()
    {
        $customer_id = $this->session->customer_id;
        $identity = $this->session->customer_info['identity'];
        $post = $this->input->post();
        $data['checked'] = $post['checked'];
        if ($post['id']) {
            $where = array(
                'customer_id' => $customer_id,
                'id' => $post['id']
            );

            $res = $this->db->where($where)->update('app_cart', $data);
            if ($res) {
                send_json(array('code' => 0, 'data' => '', 'msg' => ""));
            }
            send_json(array('code' => -1, 'data' => '', 'msg' => "操作失败"));
        } else {
            $where = array(
                'customer_id' => $customer_id,
            );
            $res = $this->db->where($where)->update('app_cart', $data);
            if ($res) {
                send_json(array('code' => 0, 'data' => '', 'msg' => ""));
            }
            send_json(array('code' => -1, 'data' => '', 'msg' => "操作失败"));
        }
    }

    //修改购物车商品数量
    public function save_good_num()
    {
        $post = $this->input->post();
        $data['num'] = $post['quantity'];
        $where = array(
            'id' => $post['goodsId']
        );
        $res = $this->db->where($where)->update('app_cart', $data);
        if ($res) {
            send_json(array('code' => 0, 'data' => '', 'msg' => ""));
        }
        send_json(array('code' => -1, 'data' => '', 'msg' => "操作失败"));
    }

    //删除购物车
    public function del()
    {
        $post = $this->input->post();
        foreach ($post['ids'] as $val) {
            $where = array(
                'id' => $val['goodsId']
            );
            $res = $this->db->where($where)->delete('app_cart');
        }
        if ($res) {
            $this->session->set_flashdata('msg', '删除成功');
            send_json(array('code' => 0, 'data' => '', 'msg' => ""));
        }
        send_json(array('code' => -1, 'data' => '', 'msg' => "操作失败"));
    }

    //企业站添加到购物车
    public function add()
    {
        $customer_id = $this->session->customer_id;
        $identity = $this->session->customer_info['identity'];
        $data = $this->input->post();
        $product_id = $data['product_id'];
        $num = $data['count'];
        $flag = 1;
        $res = $this->cart_model->add_cart($customer_id, $product_id, $num);
        if (!$res) {
            $flag = 0;
        }
        if ($flag) {
            $this->db->trans_commit();
            send_json(array('code' => 0, 'data' => $res, 'msg' => "添加购物车成功"));
        } else {
            $this->db->trans_rollback();
            send_json(array('code' => -1, 'data' => '', 'msg' => "添加购物车失败"));
        }
    }

    // 商城加入商品到购物车
    public function add_cart_api()
    {
        $post = $this->input->post();
        if (check_param($post, ['shop_user_id', 'goods_list'])) {
            $res = $this->db->query('select id as customer_id from app_customer where shop_user_id = ' . $post['shop_user_id'])->row_array();
            if (!$res['customer_id']) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "id不存在"));
            }
            $customer_id = $res['customer_id'];
            $flag = 1;
            $goods_list = json_decode($post['goods_list'], true);
            foreach ($goods_list as $good) {
                $product_id = $good['product_id'];
                $num = $good['num'];
                $res = $this->cart_model->add_cart($customer_id, $product_id, $num);
                if (!$res) {
                    $flag = 0;
                }
            }

            if ($flag) {
                $this->db->trans_commit();
                send_json(array('code' => 0, 'data' => '', 'msg' => "添加成功"));
            } else {
                $this->db->trans_rollback();
                send_json(array('code' => -1, 'data' => '', 'msg' => "添加失败"));
            }
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "缺少参数"));
        }
    }

    // 商城删除购物车的商品
    public function delete_cart_good_api()
    {
        $post = $this->input->post();
        if (check_param($post, ['shop_user_id', 'product_id'])) {
            $customer_id = $this->user_model->shop_user_id_to_customer_id($post['shop_user_id']);
            $product_id = $post['product_id'];
            $res = $this->cart_model->delete_good_from_cart($product_id, $customer_id);
            if ($res) {
                send_json(array('code' => 0, 'data' => '', 'msg' => "删除成功"));
            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => "删除失败"));
            }
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "缺少参数"));
        }

    }

    // 商城修改购物车商品选中状态
    public function change_cart_good_check_status_api()
    {
        $post = $this->input->post();
        if (check_param($post, ['shop_user_id', 'product_id', 'status'])) {
            if ($post['status'] == 1) {
                $status = 1;
            } else {
                $status = 0;
            }
            $customer_id = $this->user_model->shop_user_id_to_customer_id($post['shop_user_id']);
            $res = $this->db->where(array("customer_id" => $customer_id, "product_id" => $post['product_id']))->update('app_cart', ['checked' => $status]);
            if ($res) {
                send_json(array('code' => 0, 'data' => '', 'msg' => "修改成功"));
            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => "修改失败"));
            }
        }
    }

}